@extends('admin.layouts.content')
@section('content')
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" onsubmit="return false;">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">请输入名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" id="name" placeholder="请输入名称" autocomplete="off" class="layui-input">
                        </div>
                    </div>


                    <div class="layui-inline">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="query-form" style="height: 34px; line-height: 34px;">查询</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <table class="layui-hide" id="data-table" lay-filter="data-table"></table>


    <script type="text/html" id="data-table-top-bar" lay-filter="data-table-top-bar">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="add">添加权限</button>
        </div>
    </script>

{{--    <script type="text/html" id="data-table-bar">--}}
{{--        @{{# if((d.group_id != '' || d.group_id != null) && d.group_id !=0 ) { }}--}}
{{--        <a class="layui-btn layui-btn-xs" lay-event="edit">解绑</a>--}}
{{--        @{{# } }}--}}
{{--    </script>--}}



@endsection
@section('script')
    <script>
        var ids=''
        var names=''  //用户名
        var usernames=''  //工号
        function processs_callback(s, obj,obj_names,obj_usernames){
            if(s==1){
                ids = obj;
                names=obj_names;
                usernames=obj_usernames;
            }
        }
        layui.use(['table', 'jquery', 'form', 'upload'], function(){
            $ = layui.jquery;
            var table = layui.table;
            var form = layui.form;
            var upload = layui.upload;

            var tableIns = table.render({
                elem: '#data-table'

                ,url:'{{url('admin/api/user/apiList')}}'
                ,page: true
                ,cellMinWidth: 80
                ,limit: 10
                ,where: { name: '' }
                ,cols: [[
                    {field:'id', title:'序号', width:100,  sort: true},
                    {field:'user_id', title:'用户id', width:100,  sort: true}
                    ,{field:'username', title: '用户名', minWidth: 100}
                    ,{field:'loginname', title: '工号', minWidth: 100}
                    // ,{field: 'group_id',title: '状态',  minWidth: 100,templet:function (d) {
                    //         if((d.group_id !='' || d.group_id !=null) && d.group_id !=0 ){
                    //             return '已绑定'
                    //         }
                    //         return '已解绑'
                    //     }}
                    ,{field:'create_time', title: '创建时间', width: 180}
                    // ,{title: '操作', toolbar: '#data-table-bar', width: 450, fixed:'right'}
                ]]
                ,toolbar: "#data-table-top-bar"
                ,defaultToolbar: ['filter', 'exports', 'print']
                // ,parseData: function (res) {
                //     return {
                //         "code": 0, //解析接口状态
                //         "msg": "获取成功", //解析提示文本
                //         "count": res.data.total, //解析数据长度
                //         "data": res.data.data //解析数据列表
                //     };
                // }
            });
            form.on('submit(query-form)', function () {
                eventMap.tableReload();
            });

            table.on('tool(data-table)', function(obj){
                switch (obj.event) {
                    case 'delete':
                        eventMap.delete(obj);
                        break;
                    case 'edit':
                        eventMap.edit(obj, form);
                        break;
                    case 'business':
                        eventMap.business(obj);
                        break;
                    case "clone":
                        eventMap.clone(obj);
                        break;
                    case "link":
                        eventMap.link(obj);
                        break;
                    case "publish":
                        eventMap.publish(obj);
                        break;
                    default:
                        layer.msg(obj.event);
                }
            });
            table.on('toolbar(data-table)', function (obj) {
                if (obj.event == 'add') {
                    eventMap.add(obj);
                }
            });

            var eventMap = {
                add: function (form) {
                    layer.open({
                        type: 2,
                        title: "添加权限",
                        content: '{{url('admin/api/user/select')}}',
                        area: ['80%', '80%'],
                        btn: ['确定', '关闭'],
                        btn1: function (index, layero) {

                            // var iframeWindow = window['layui-layer-iframe' + index]  //获取弹框页面
                            //
                            // console.log(iframeWindow);
                            //
                            //
                            // var checkStatus = iframeWindow.layui.table.checkStatus('product_data-table');//获取选中行
                            // console.log(checkStatus);
                            //
                            // if(checkStatus.data.length<=0){
                            //     layer.msg('请勾选用户',{icon:5})
                            //     return false;
                            // }else{
                            //     var user_id='';  //商品表id
                            //
                            //     for ( var k in checkStatus.data) {
                            //
                            //         user_id +=checkStatus.data[k].id+',';
                            //
                            //     }
                            //
                            // }
                            // user_id=user_id.substr(0,user_id.length-1);

                            $.ajax({
                                url:'{{url('admin/api/user/UserPmsCreate')}}',
                                data:{
                                    user_id:ids,
                                    name:names,
                                    username:usernames,
                                    _token: "{{ csrf_token() }}"
                                },
                                dataType:'json',
                                type:"post",
                                success:function (result){
                                    if(result.error == 0) {
                                        layer.msg(result.msg);
                                        parent.layer.close(index);
                                        tableIns.reload();
                                        //重载该实例，支持重载全部基础参数

                                    }
                                    else {
                                        layer.msg(result.msg, { 'icon': 2 });
                                    }
                                    layer.close(index);
                                }
                            })
                        }
                    });
                }
                ,edit: function (obj, form) {
                    layer.confirm('您确定要解绑吗？', {
                        btn: ['确认', '取消']
                    }, function(index, layero){
                        $.ajax({
                            url : '{{url('admin/api/user/Unbound')}}',
                            type : "POST",
                            data : {
                                user_id : obj.data.user_id,
                                //status: 2,
                                _token: "{{ csrf_token() }}"
                            },
                            dataType : 'json',
                            success : function(result) {
                                if(result.error == 0) {
                                    layer.msg(result.msg);
                                    tableIns.reload();
                                }
                                else {
                                    layer.msg(result.msg, { 'icon': 2 });
                                }
                            }
                        });
                    }, function(index){
                        layer.closeAll();
                    });
                }




                ,delete: function (obj) {
                    layer.confirm('您确定要删除此报表吗？', {
                        btn: ['确认', '取消']
                    }, function(index, layero){
                        $.ajax({
                            url : '{{url('admin/api/report/delete')}}',
                            type : "POST",
                            data : {
                                id : obj.data.id,
                                status: 2,
                                _token: "{{ csrf_token() }}"
                            },
                            dataType : 'json',
                            success : function(result) {
                                if(result.error == 0) {
                                    layer.msg(result.msg);
                                    tableIns.reload();
                                }
                                else {
                                    layer.msg(result.msg, { 'icon': 2 });
                                }
                            }
                        });
                    }, function(index){
                        layer.closeAll();
                    });
                }
                ,tableReload: function () {
                    var name = $("#name").val();
                    tableIns.reload({
                        where: {name: name}
                        ,page: {
                            curr: 1
                        }
                    });
                    $("#name").val(name);
                }
            };
        });
    </script>
@endsection
